package com.tarena.lbs.basic.web.service.impl;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;

import com.tarena.lbs.basic.web.repository.DictItemRepository;
import com.tarena.lbs.basic.web.repository.DictRepository;
import com.tarena.lbs.basic.web.service.IDictItemService;
import com.tarena.lbs.common.basic.converter.DictItemConverter;
import com.tarena.lbs.common.basic.enums.DictStatusEnum;
import com.tarena.lbs.pojo.basic.bo.DictItemBO;
import com.tarena.lbs.pojo.basic.dos.DictItemDO;
import com.tarena.lbs.pojo.basic.param.DictItemParam;
import com.tarena.lbs.pojo.basic.query.DictItemQuery;
import com.tarena.lbs.pojo.basic.vo.DictItemVO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
public class DictItemServiceImpl implements IDictItemService {
    @Autowired
    private DictItemRepository dictItemRepository;


    @Autowired
    private DictRepository dictRepository;

    @Autowired
    private DictItemConverter dictItemConverter;


    @Override
    @Transactional()
    public void save(DictItemParam dictItemParam) throws BusinessException {

        //查询dict_id  +  dictvalue  唯一
        checkUniqueByDictIdAndDictValue(null, dictItemParam, "yes");

        DictItemDO dictItemDO = new DictItemDO();
        BeanUtils.copyProperties(dictItemParam, dictItemDO);
        Date date = new Date();
        //设置创建时间以及更新时间
        dictItemDO.setCreateTime(date);
        dictItemDO.setUpdateTime(date);
        dictItemRepository.insert(dictItemDO);

    }

    private void checkUniqueByDictIdAndDictValue(Integer id, DictItemParam dictItemParam, String self) throws BusinessException {
        DictItemQuery dictItemQuery = new DictItemQuery();
        dictItemQuery.setDictId(dictItemParam.getDictId());
        dictItemQuery.setDictValue(dictItemParam.getDictValue());
        dictItemQuery.setSelf(self);
        if (Objects.nonNull(id)) {
            dictItemQuery.setId(id);
        }
        List<DictItemDO> dictItemDOS = dictItemRepository.selectByCondition(dictItemQuery);

        if(!CollectionUtils.isEmpty(dictItemDOS)){
            throw new BusinessException("10013","字典数据类型不能重复");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        dictItemRepository.delete(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Integer id, DictItemParam dictItemParam) throws BusinessException {
        checkUniqueByDictIdAndDictValue(id, dictItemParam, "no");
        DictItemDO dictItemDO = new DictItemDO();
        BeanUtils.copyProperties(dictItemParam, dictItemDO);
        dictItemDO.setId(id);
        dictItemDO.setUpdateTime(new Date());
        dictItemRepository.update(dictItemDO);

    }

    @Override
    public DictItemBO detail(Integer id) {
       DictItemBO dictItemBO = dictItemRepository.detail(id);
        if (null == dictItemBO) {
            return null;
        }
        return dictItemBO;
    }

    @Override
    public PageResult<DictItemVO> list(DictItemQuery dictItemQuery) {
        //加入分页
        int page = dictItemQuery.getPageNo();
        int pageSize = dictItemQuery.getPageSize();
        //new PageQuery(page, pageSize);
        List<Integer> statusList = new ArrayList<>();
        if (null == dictItemQuery.getStatus()) {
            statusList.add(DictStatusEnum.ACTIVE.getCode());
            statusList.add(DictStatusEnum.NOT_ACTIVE.getCode());
        } else {
            statusList.add(dictItemQuery.getStatus());
        }
        dictItemQuery.setStatusList(statusList);
        //加入字典类型
        int dictId = dictItemQuery.getDictId();
        dictItemQuery.setDictId(dictId);

        List<DictItemDO> dictItemDOS = dictItemRepository.selectByCondition(dictItemQuery);
        PageInfo<DictItemDO> pageInfo = new PageInfo<>(dictItemDOS);
        List<DictItemVO> dictItemVOList = pageInfo.getList().stream().map(DictItemDO -> {
            DictItemVO DictItemVO = new DictItemVO();
            BeanUtils.copyProperties(DictItemDO, DictItemVO);
            return DictItemVO;
        }).collect(Collectors.toList());

        PageResult<DictItemVO> pageResult = new PageResult<>();
        pageResult.setObjects(dictItemVOList);
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setPageNo(page);
        pageResult.setPageSize(pageSize);
        return pageResult;
    }

    @Override
    public List<DictItemBO> getlistAll(Integer dictId) {
        DictItemQuery dictItemQuery = new DictItemQuery();
        //加入字典类型
        dictItemQuery.setDictId(dictId);
        List<DictItemDO> dictItemDOS = dictItemRepository.selectByCondition(dictItemQuery);
        List<DictItemBO> dictItemBOS = dictItemConverter.doList2boList(dictItemDOS);
        return dictItemBOS;
    }
}
